<?php
require_once("../core/lib/phpmailer/class.phpmailer.php");

class LandingpageController extends MainController implements ControllerInterface {
	
	public function IndexValidationAction($param, $as_action = true) {	
		if ($as_action)
				$param['validation_data'] = array();	
		
		$validator = new Validator($param['validation_data']);
		$validator->IsEmailAddress('emailAddr', 'Please enter email address');
		
		$this->smarty->assign('js_values', $validator->getJsValues());
		$this->smarty->assign('id_form', Request::GetGet('id_f'));
		return $validator->getErrorList();		
	}
	
	public function indexAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");
		
		
		if(Request::IsPost()) {
			$this->sendMail(Request::GetPost('emailAddr'), Request::GetPost('senderMessage'));
		} else {
			$this->AddScript('jquery-validate/jquery.validate.js');
			$this->AddScript(Router::GenerateUrl('landingPageIndexValidation') . '?id_f=landingForm1');		
		}
		
		$template = 'landingpage.tpl';
		Registry::Remove('smartyTemplate');
		Registry::Set('smartyTemplate', $template);

	}
	
	public function PassionAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");	
				$template = 'landingpage.tpl';
		Registry::Remove('smartyTemplate');
		Registry::Set('smartyTemplate', $template);
	}
	
	public function PhilosophyAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");
				$template = 'landingpage.tpl';
		Registry::Remove('smartyTemplate');
		Registry::Set('smartyTemplate', $template);
	}
	
	public function ServicesAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");
				$template = 'landingpage.tpl';
		Registry::Remove('smartyTemplate');
		Registry::Set('smartyTemplate', $template);
	}


	private function sendMail($arrAddresses, $body = '') {
		$mail = new PHPMailer();
		$mail->IsSMTP();
		$mail->FromName = "notify@tennizoom.com";
		$mail->From     = "notify@tennizoom.com";
		$mail->Host     = "tennizoom.com";
		$mail->Username = "notify@tennizoom.com";
		$mail->Password = "TenniZ00m_Notify2011";
		$mail->SMTPAuth = true;
		$mail->IsHTML(1);
		
		if(!is_array($arrAddresses))
			$arrAddresses = array($arrAddresses);
		
		foreach($arrAddresses as $item) {
			$mail->AddAddress($item);
		}
		
		$mail->Subject = $this->smarty->fetch('partial/Landingpage/mail/subject.tpl');
		
		if(empty($body))
			$mail->Body = $this->smarty->fetch('partial/Landingpage/mail/body.tpl');
		else
			$mail->Body = $body;
		
		if(!$mail->Send()) {
			throw new Exception('Mailer error: ' . $mail->ErrorInfo);
		} else {
			$this->AddRedirectInfo('/');
		}
	}
	
	public function PreDispatch($param) {
		$this->Run($param);

	}

	public function PostDispatch($param) {
		
		// nagłówek strony
		$this->RunShared('Header', $param);
		
		// footer
		$this->RunShared('Footer', $param);
	}
}
?>